<template>
    <div>
        <div v-if="state === -1" style="margin: 0 auto;ext-align: center" >
            <h1 style="text-align: center">审核不通过</h1>
            <span slot="footer" class="dialog-footer" style="margin-left: 550px" >
				<el-button  type="primary" @click="searchAudit">查看审核信息</el-button>
				<el-button type="primary" @click="againAudit">重新填写入驻信息</el-button>
			  </span>
        </div>
        <div v-else-if="state === 0">待审核</div>
        <div v-else-if="state === 1">
            <h1 style="text-align: center">审核通过</h1>
            <span slot="footer" class="dialog-footer" style="margin-left: 650px" >
				<el-button  type="primary" @click="activation">激活店铺</el-button>
            </span>
        </div>
        <div v-else="state === 2">禁用</div>


        <el-dialog title="商铺信息" :visible.sync="dialogFormVisible">
            <el-form :model="shop" ref="shopForm" :rules="formRules" label-position="left" label-width="100px" class="demo-ruleForm login-container">
                <h3 class="title">店铺入驻</h3>
                <el-form-item prop="name"label="名称">
                    <el-input type="text" v-model="shop.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
                </el-form-item>
                <el-form-item prop="tel" label="座机">
                    <el-input type="text" v-model="shop.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
                </el-form-item>
                <el-form-item prop="address" label="店铺地址">
                    <el-input type="text" style="width: 350px" v-model="shop.address" auto-complete="off" placeholder="请输入地址！"></el-input>
                    <!--        <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>-->
                </el-form-item>
                <el-form-item prop="logo" label="店铺Logo">
                    <el-upload
                            class="upload-demo"
                            action="http://localhost/fastDfs/"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <h3>管理员信息设置</h3>
                <el-form-item prop="username" label="账号">
                    <el-input type="text" v-model="this.employeeName.username" auto-complete="off" placeholder="请输入账号！"></el-input>
                </el-form-item>
                <el-form-item prop="tel" label="手机号码">
                    <el-input type="text" v-model="this.employeeName.phone" auto-complete="off" placeholder="请输入电话！"></el-input>
                </el-form-item>
                <el-form-item prop="email" label="电子邮件">
                    <el-input type="text" v-model="this.employeeName.email" auto-complete="off" placeholder="请输入邮件！"></el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码">
                    <el-input type="password" v-model="this.employeeName.password" auto-complete="off" placeholder="请输入密码！"></el-input>
                </el-form-item>
                <el-form-item prop="comfirmPassword" label="确认密码">
                    <el-input type="password" v-model="this.employeeName.password" auto-complete="off" placeholder="请输入确认密码！"></el-input>
                </el-form-item>
                <el-form-item style="width:100%;">
                    <el-button type="primary" style="width:100%;" @click.native.prevent="settledIn" >入驻</el-button>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>

    export default {
        data() {
            return {
                //获取当前店铺状态
                state: "",
                //入驻信息对话框默认关闭
                dialogFormVisible:false,
                fileList: [],
                //入驻信息表单
                shop:{
                    name: '',
                    tel: '',
                    address: '',
                    logo:''
                },
                employeeName:{
                    username:'',
                    tel:'',
                    phone:'',
                    email:'',
                    password:'',
                    comfirmPassword:''
                },
                formRules: {
                    name: [
                        {required: true, message: '请输入店铺名称!', trigger: 'blur'}
                    ],
                }

            }
        },
        methods:{
            //激活店铺
            activation(){
                //跳转登录页面
                this.$router.push({ path: '/' });
            },
            //图片保存成功
            handleSuccess(response, file, fileList){
                //返回fastdfs存储路径
                this.shop.logo = response.result;
            },
            //图片移除
            handleRemove(file, fileList) {
                let filePath =file.response.result;
                this.$http.delete("/fastDfs/?path="+filePath)
                    .then(res=>{
                        if(res.data.success){
                            this.$message({
                                message: '删除成功!',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '删除失败!',
                                type: 'error'
                            });
                        }
                    })
            },
            //查询审核信息
            searchAudit(){
                this.$message.success("审核不通过，因为商铺名称过长！");
            },
            //重新填写入驻信息
            againAudit(){
                //打开表单
                this.dialogFormVisible = true;
                let id = 38;
                this.$http.patch("/shop/loadById/" + id).then((res => {
                    //获取表单回显的值
                    this.shop = res.data;
                    this.employeeName = res.data.employeeName;
                }));




            },
            settledIn(){
                this.$refs.shopForm.validate((valid) => {
                    //校验表单成功后才做一下操作
                    if (valid) {
                        this.$confirm('确认入驻吗？', '提示', {}).then(() => {
                            //拷贝后面对象的值到新对象,防止后面代码改动引起模型变化
                            let para = Object.assign({}, this.shop); //shop 本身这个参数里面就有店铺和管理员信息
                            // 为了后台好接收，封装一个对象放到里面
                            let admin = {
                                username: para.username,
                                phone: para.phone,
                                email: para.email,
                                password:para.password,
                                comfirmPassword:para.comfirmPassword
                            }
                            para.admin = admin;
                            //判断是否有id有就是修改,否则就是添加
                            this.$http.post("/shop/settledIn",para).then((res) => {
                                if(res.data.success){
                                    this.$message({
                                        message: '操作成功!',
                                        type: 'success'
                                    });
                                    //重置表单
                                    this.$refs['shopForm'].resetFields();
                                    //跳转登录页面
                                    this.$router.push({ path: '/login' });
                                }
                                else{
                                    this.$message({
                                        message: res.data.message,
                                        type: 'error'
                                    });
                                }

                            });
                        });
                    }
                })
            }
        },
        //页面加载的时候执行
        mounted() {
            //设置固定id
            let id = 38;
            //查询审核状态
            this.$http.patch("/shop/view/" + id).then((res => {
                //获取到状态
                this.state = res.data.state;
            }));
        }
    }
</script>


